package com.matrix.app.flux.project.stage.repository;

import com.matrix.app.flux.project.stage.data.StagePo;
import org.springframework.data.domain.Pageable;
import org.springframework.data.r2dbc.repository.Query;
import org.springframework.data.r2dbc.repository.R2dbcRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

/**
 * 项目阶段持久层
 *
 * Copyright © 海平面工作室 版权所有
 *
 * @Author: Leo
 * @Create: 2025/06/02 10:00
 * @Since 1.2
 */
@Repository
public interface StageRepository extends R2dbcRepository<StagePo, Long> {
    
    /**
     * 按条件分页查询阶段
     * @param searchKey 名称或备注关键字
     * @param pageable 分页参数
     * @return 分页结果
     */
    @Query("SELECT * FROM project_stage WHERE (:searchKey IS NULL OR name LIKE CONCAT('%', :searchKey, '%') OR memo LIKE CONCAT('%', :searchKey, '%')) ORDER BY id")
    Flux<StagePo> findByConditions(@Param("searchKey") String searchKey, Pageable pageable);
    
    /**
     * 按条件统计总数
     * @param searchKey 名称或备注关键字
     * @return 总数
     */
    @Query("SELECT COUNT(*) FROM project_stage WHERE (:searchKey IS NULL OR name LIKE CONCAT('%', :searchKey, '%') OR memo LIKE CONCAT('%', :searchKey, '%'))")
    Mono<Long> countByConditions(@Param("searchKey") String searchKey);
} 